package com.jlnk.glidedemo.views;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlnk.glidedemo.R;
import com.jlnk.glidedemo.adapter.ImageAdapter;
import com.jlnk.glidedemo.entity.ImageEntity;
import com.jlnk.glidedemo.presenter.ImageListPresenter;

import java.util.List;

public class ImageListActivity extends AppCompatActivity implements ImageListView, ImageAdapter.OnItemClickListener {
    private RecyclerView recyclerView;
    private ImageAdapter imageAdapter;
    private ImageListPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_list);

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        presenter = new ImageListPresenter(this);
        presenter.getImages();
    }

    @Override
    public void showImages(List<ImageEntity> imageList) {
        imageAdapter = new ImageAdapter(imageList, this, this);
        recyclerView.setAdapter(imageAdapter);
    }

    @Override
    public void showError() {
    }

    @Override
    public void openImageDetail(String imageUrl) {
        Intent intent = new Intent(this, ImageDetailActivity.class);
        intent.putExtra("imageUrl", imageUrl);
        startActivity(intent);
    }

    @Override
    public void onItemClick(ImageEntity imageEntity) {
        presenter.openImageDetail(imageEntity.getImageUrl());
    }
}